
package com.thon.service.util.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.thon.entity.project.Data;
import com.thon.service.util.DateAnalysis;
import com.thon.commons.utils.CollectionToCollection;
/**
 * @file DateAnalysisImpl.java
 * @author manpiaoyi
 * @email manpiaoyi@126.com
 * @date 2014-11-10
 * @description 时间分析器
 */

public class DatabaseDateAnalysisImpl extends DateAnalysis<Data> {		

	

	private Integer[] typeArray;
	
	public DatabaseDateAnalysisImpl(Date startDate,Date endDate,String types)
	{
		super(startDate,endDate);		
		typeArray = CollectionToCollection.strToIntArr(types, ",");	
	}



	@Override
	public List<Data> dataAnalysis(List<Data> datum) {
		if(datum==null)
			return null;	
		
		preAnalysis();			
			
		return doAnalysis(datum);
	}

	public Integer getMethod() {
		return super.method;
	}

	@Override
	public List<Data> doAnalysis(List<Data> datum) {
		
		
		Calendar tempDate =  Calendar.getInstance();
		tempDate.setTime(datum.get(0).getCreatDate());	
		Calendar date =null;
		Data[] newData = new Data[typeArray.length];
		List<Data> newDatum = new ArrayList<Data>();
	
		
		for(Data data:datum)
		{		
			date =  Calendar.getInstance();
			date.setTime(data.getCreatDate());
			if(tempDate.get(method)!=date.get(method))
			{				
				for(int i=0;i<typeArray.length;++i)
				{
					if(newData[i]!=null)
						newDatum.add(newData[i]);
					newData[i] = null;
				}
				
				tempDate = date;
				
			}			
			
			int ind = CollectionToCollection.findIndex(typeArray, data.getType().getId());
			
			if(newData[ind]==null)
				{ 
					newData[ind] = (Data) data.clone();
				}
			else
				newData[ind].setValue(newData[ind].getValue()+data.getValue()); 
			
		}
		
		for(int i=0;i<typeArray.length;++i)
		{
			if(newData[i]!=null)
				newDatum.add(newData[i]);
			newData[i] = null;
		}
		
		
		return newDatum;
		
	}
	
	
}

		
	

	
	
